﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using MyAspNetCoreTodo.Dtos;
using MyAspNetCoreTodo.Identity;
using MyAspNetCoreTodo.Models;
using MyAspNetCoreTodo.Services;

namespace MyAspNetCoreTodo.Controllers
{
    [Authorize]
    public class TodoController : Controller
    {
        // GET: TodoController

        private readonly ITodoItemService _todoItemService; 
        private readonly UserManager<ApplicationUser> _userManager;
        public TodoController(ITodoItemService todoItemService,  UserManager<ApplicationUser> userManager)
        {
            _todoItemService = todoItemService;
            
            _userManager = userManager;
        }
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var currentUser = await _userManager.GetUserAsync(User);
            if (currentUser == null) return Challenge();
            //var Users= await _userService.CreateTestUser();

            // 从数据库获取 to-do 条目
            var items = await _todoItemService.GetIncompleteItemsAsync(currentUser);
            // 把条目置于 model 中

            ToDoViewModel viewModel = new ToDoViewModel()
            {
                Items = items 
            };

            // 使用 model 渲染视图
            return View(viewModel);
        }

        // GET: TodoController/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: TodoController/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: TodoController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(TodoItemDto model)
        {
            var currentUser = await _userManager.GetUserAsync(User);
            if (currentUser == null) return Challenge();

            if (ModelState.IsValid)
            {
                var success = await _todoItemService.AddItemAsync(model,currentUser);
                if (success)
                {
                    return RedirectToAction(nameof(Index));
                }
                else
                {
                    ModelState.AddModelError("", "Failed to add the todo item.");
                }
            }

            return View(model);
        }
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> MarkDone(Guid id)
        {
            var curUser = await _userManager.GetUserAsync(User);
            if (curUser==null)
            {
                return Challenge();
            }
            if (id==Guid.Empty)
            {
                return RedirectToAction("Index");
            }

            var suffult = await _todoItemService.MarkDoneAsync(id,curUser);
            if (suffult)
            {
                return RedirectToAction(nameof(Index));
            }
            else
            {
                return BadRequest("Could not mark item as done.");
            }
        }

        [ValidateAntiForgeryToken]
        public async Task<IActionResult> AddItem(TodoItemDto newItem)
        {

            var curUser =  await _userManager.GetUserAsync (User);
            if (curUser==null)
            {
                return Challenge();
            }
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }

            var successful = await _todoItemService.AddItemAsync(newItem,curUser);
            if (!successful)
            {
                return BadRequest("Could not add item.");
            }

            return RedirectToAction("Index");
        }

        // GET: TodoController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: TodoController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: TodoController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: TodoController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
